package com.ruoyi.web.controller.app;

import com.ruoyi.assets.domain.StateCollection;
import com.ruoyi.assets.service.IStateCollectionService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 小程序收藏Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/app/collection")
public class AppCollectionController extends BaseController {
    @Autowired
    private IStateCollectionService stateCollectionService;

    /**
     * 获取当前用户收藏列表
     */
    @GetMapping("/list")
    public TableDataInfo<StateCollection > list(
            @RequestParam(value = "assetStatus", required = false) Integer assetStatus,
            @RequestParam(value = "keyword", required = false) String keyword) {
        Long userId = SecurityUtils.getUserId2();
        startPage();
        StateCollection collection = new StateCollection();
        collection.setUserId(userId);
        
        // 设置资产状态过滤条件（通过参数传递给mybatis）
        if (assetStatus != null) {
            collection.getParams().put("assetStatus", assetStatus);
        }
        
        // 设置关键词搜索条件
        if (keyword != null && !keyword.isEmpty()) {
            collection.getParams().put("keyword", keyword);
        }
        
        List<StateCollection> list = stateCollectionService.selectStateCollectionList(collection);
        return getDataTable(list);
    }

    /**
     * 添加收藏
     */
    @Log(title = "小程序收藏", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Map<String, Long> params) {
        Long userId = SecurityUtils.getUserId2();
        Long assetId = params.get("assetId");
        if (assetId == null) {
            return error("参数错误");
        }

        return toAjax(stateCollectionService.addCollection(userId, assetId));
    }

    /**
     * 取消收藏
     */
    @Log(title = "小程序收藏", businessType = BusinessType.DELETE)
    @PostMapping("/cancel")
    public AjaxResult cancel(@RequestBody Map<String, Long> params) {
        Long userId = SecurityUtils.getUserId2();
        Long assetId = params.get("assetId");
        if (userId == null || assetId == null) {
            return error("参数错误");
        }
        return toAjax(stateCollectionService.cancelCollection(userId, assetId));
    }
    
    /**
     * 检查是否已收藏
     */
    @Anonymous
    @GetMapping("/check")
    public AjaxResult check(@RequestParam(value = "userId", required = false) Long userId, @RequestParam(value = "assetId", required = false) Long assetId) {
        if (userId == null) {
            return success(false);
        }
        if (assetId == null) {
            return error("参数错误");
        }
        boolean isCollected = stateCollectionService.isCollected(userId, assetId);
        return success(isCollected);
    }
} 